# 基本配置
TARGET      := pcie_mcdev
BUILD_DIR   := ./build
SRC_DIR     := ./src
INC_DIRS    := ./inc

# 硬件版本定义
HARD_VER ?= 0

# 编译模式：可选值 cross(交叉编译) 或 native(本地编译)
COMPILE_MODE ?= cross

# 芯片平台：可选值 rk3588, rk3568
SOC_PLATFORM ?= rk3568

# 根据编译模式和芯片平台设置工具链和内核路径
ifeq ($(COMPILE_MODE), cross)
    $(info Building for $(SOC_PLATFORM) using cross-compilation)
    ifeq ($(SOC_PLATFORM), rk3588)
        CROSS_COMPILE   ?= /media/test/sde/ubuntu2204/rk3588/tl3588-evm_buildroot-2021.11_v2.2/rk3588_linux_release/prebuilts/gcc/linux-x86/aarch64/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu-
        CFLAGS          += -DTARGET_RK3588
    else ifeq ($(SOC_PLATFORM), rk3568)
        CROSS_COMPILE  	?= /media/test/sde/ubuntu2204/rk3568/tl3568f-evm_v1.5/rk356x_linux_release_v1.3.1/prebuilts/gcc/linux-x86/aarch64/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu-
        CFLAGS          += -DTARGET_RK3568
    else
        $(error Unsupported SOC_PLATFORM for cross compilation: $(SOC_PLATFORM). Supported values: rk3588, rk3568)
    endif
else ifeq ($(COMPILE_MODE), native)
    $(info Building natively for $(SOC_PLATFORM))
    CROSS_COMPILE       ?=
    ifeq ($(SOC_PLATFORM), rk3588)
        CFLAGS          += -DTARGET_RK3588
    else ifeq ($(SOC_PLATFORM), rk3568)
        CFLAGS          += -DTARGET_RK3568
    else
        $(error Unsupported SOC_PLATFORM for native compilation: $(SOC_PLATFORM). Supported values: rk3588, rk3568)
    endif
else
    $(error Unsupported COMPILE_MODE: $(COMPILE_MODE). Supported values: cross, native)
endif
CC                      := $(CROSS_COMPILE)gcc
AR                      := $(CROSS_COMPILE)ar

# 版本信息
LIB_VER_MAJOR := 1
LIB_VER_MINOR := 0
LIB_VER_PATCH := 0
CANFD_VER_MAJOR := $(LIB_VER_MAJOR)
CANFD_VER_MINOR := $(LIB_VER_MINOR)
CANFD_VER_PATCH := $(LIB_VER_PATCH)
CAN_VER_MAJOR := $(LIB_VER_MAJOR)
CAN_VER_MINOR := $(LIB_VER_MINOR)
CAN_VER_PATCH := $(LIB_VER_PATCH)
DI_VER_MAJOR := $(LIB_VER_MAJOR)
DI_VER_MINOR := $(LIB_VER_MINOR)
DI_VER_PATCH := $(LIB_VER_PATCH)
DO_VER_MAJOR := $(LIB_VER_MAJOR)
DO_VER_MINOR := $(LIB_VER_MINOR)
DO_VER_PATCH := $(LIB_VER_PATCH)
AI_VER_MAJOR := $(LIB_VER_MAJOR)
AI_VER_MINOR := $(LIB_VER_MINOR)
AI_VER_PATCH := $(LIB_VER_PATCH)
AO_VER_MAJOR := $(LIB_VER_MAJOR)
AO_VER_MINOR := $(LIB_VER_MINOR)
AO_VER_PATCH := $(LIB_VER_PATCH)
PI_VER_MAJOR := $(LIB_VER_MAJOR)
PI_VER_MINOR := $(LIB_VER_MINOR)
PI_VER_PATCH := $(LIB_VER_PATCH)
PT_VER_MAJOR := $(LIB_VER_MAJOR)
PT_VER_MINOR := $(LIB_VER_MINOR)
PT_VER_PATCH := $(LIB_VER_PATCH)

# 库文件名定义
STATIC_LIB  := lib$(TARGET).a
DYNAMIC_LIB_REALNAME    := lib$(TARGET).so.$(LIB_VER_MAJOR).$(LIB_VER_MINOR).$(LIB_VER_PATCH)
DYNAMIC_LIB_SONAME      := lib$(TARGET).so.$(LIB_VER_MAJOR)
DYNAMIC_LIB_LINKNAME    := lib$(TARGET).so

# 编译选项
LDFLAGS_DYN :=  -shared -lm -lpthread -Wl,-soname,$(DYNAMIC_LIB_SONAME)
CFLAGS      +=  -Wall -Wextra -I$(INC_DIRS) -fPIC  # 静态库和动态库都需要 -fPIC
CFLAGS      +=  -DLOG_LEVEL=$(LOG_LEVEL)
CFLAGS      +=  -DCANFD_VER_MAJOR=$(CANFD_VER_MAJOR) \
                -DCANFD_VER_MINOR=$(CANFD_VER_MINOR) \
                -DCANFD_VER_PATCH=$(CANFD_VER_PATCH) \
                -DCAN_VER_MAJOR=$(CAN_VER_MAJOR) \
                -DCAN_VER_MINOR=$(CAN_VER_MINOR) \
                -DCAN_VER_PATCH=$(CAN_VER_PATCH) \
                -DDI_VER_MAJOR=$(DI_VER_MAJOR) \
                -DDI_VER_MINOR=$(DI_VER_MINOR) \
                -DDI_VER_PATCH=$(DI_VER_PATCH) \
                -DDO_VER_MAJOR=$(DO_VER_MAJOR) \
                -DDO_VER_MINOR=$(DO_VER_MINOR) \
                -DDO_VER_PATCH=$(DO_VER_PATCH) \
                -DAI_VER_MAJOR=$(AI_VER_MAJOR) \
                -DAI_VER_MINOR=$(AI_VER_MINOR) \
                -DAI_VER_PATCH=$(AI_VER_PATCH) \
                -DAO_VER_MAJOR=$(AO_VER_MAJOR) \
                -DAO_VER_MINOR=$(AO_VER_MINOR) \
                -DAO_VER_PATCH=$(AO_VER_PATCH) \
                -DPI_VER_MAJOR=$(PI_VER_MAJOR) \
                -DPI_VER_MINOR=$(PI_VER_MINOR) \
                -DPI_VER_PATCH=$(PI_VER_PATCH) \
                -DPT_VER_MAJOR=$(PT_VER_MAJOR) \
                -DPT_VER_MINOR=$(PT_VER_MINOR) \
                -DPT_VER_PATCH=$(PT_VER_PATCH) \


# 工具命令
RM          := rm -rf
MKDIR       := mkdir -p
LN          := ln -sf
STRIP       := $(CROSS_COMPILE)strip

# 自动查找源文件
SRCS        := $(shell find $(SRC_DIR) -name '*.c' 2>/dev/null)
OBJS        := $(SRCS:$(SRC_DIR)/%.c=$(BUILD_DIR)/%.o)

# 默认目标 - 同时构建静态库和动态库
all: LOG_LEVEL ?= 2
all: static dynamic

# 静态库目标
static: $(BUILD_DIR)/$(STATIC_LIB)

# 动态库目标
dynamic: $(BUILD_DIR)/$(DYNAMIC_LIB_REALNAME) $(BUILD_DIR)/$(DYNAMIC_LIB_SONAME) $(BUILD_DIR)/$(DYNAMIC_LIB_LINKNAME)

# 构建静态库
$(BUILD_DIR)/$(STATIC_LIB): $(OBJS) | $(BUILD_DIR)
	$(AR) rcs $@ $(OBJS)
	@echo "=== 静态库构建完成 ==="
	@echo "静态库: $(STATIC_LIB)"

# 创建动态库符号链接
$(BUILD_DIR)/$(DYNAMIC_LIB_LINKNAME): $(BUILD_DIR)/$(DYNAMIC_LIB_SONAME)
	@echo "创建符号链接: $(DYNAMIC_LIB_LINKNAME) -> $(DYNAMIC_LIB_SONAME)"
	@cd $(BUILD_DIR) && $(LN) $(DYNAMIC_LIB_SONAME) $(DYNAMIC_LIB_LINKNAME)

$(BUILD_DIR)/$(DYNAMIC_LIB_SONAME): $(BUILD_DIR)/$(DYNAMIC_LIB_REALNAME)
	@echo "创建符号链接: $(DYNAMIC_LIB_SONAME) -> $(DYNAMIC_LIB_REALNAME)"
	@cd $(BUILD_DIR) && $(LN) $(DYNAMIC_LIB_REALNAME) $(DYNAMIC_LIB_SONAME)

# 构建动态库
$(BUILD_DIR)/$(DYNAMIC_LIB_REALNAME): $(OBJS) | $(BUILD_DIR)
	$(CC) $(OBJS) -o $@ $(LDFLAGS_DYN)
	@echo "=== 动态库构建完成 ==="
	@echo "动态库: $(DYNAMIC_LIB_REALNAME)"

# 编译源文件
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c | $(BUILD_DIR)
	@$(MKDIR) $(dir $@)
	$(CC) $(CFLAGS) -c $< -o $@

# 创建构建目录
$(BUILD_DIR):
	$(MKDIR) $@

# 清理
clean:
	$(RM) $(BUILD_DIR)

# 安装库文件到系统目录
install: all
	@echo "安装库文件到系统目录..."
	@if [ -f "$(BUILD_DIR)/$(STATIC_LIB)" ] && [ -f "$(BUILD_DIR)/$(DYNAMIC_LIB_REALNAME)" ]; then \
		# 安装静态库 \
		install -m 644 $(BUILD_DIR)/$(STATIC_LIB) /usr/local/lib/; \
		# 安装动态库 \
		install -m 755 $(BUILD_DIR)/$(DYNAMIC_LIB_REALNAME) /usr/local/lib/; \
		cd /usr/local/lib/ && $(LN) $(DYNAMIC_LIB_REALNAME) $(DYNAMIC_LIB_SONAME); \
		cd /usr/local/lib/ && $(LN) $(DYNAMIC_LIB_SONAME) $(DYNAMIC_LIB_LINKNAME); \
		# 安装头文件 \
		if [ -d "$(INC_DIRS)" ]; then \
			find $(INC_DIRS) -name '*.h' -exec install -D -m 644 {} /usr/local/include/$(TARGET)/ \; 2>/dev/null || true; \
		fi; \
		ldconfig; \
		echo "安装完成"; \
	else \
		echo "错误: 库文件未构建，请先运行 'make'"; \
	fi

# 安装仅动态库
install-dynamic: dynamic
	@echo "安装动态库到系统目录..."
	@if [ -f "$(BUILD_DIR)/$(DYNAMIC_LIB_REALNAME)" ]; then \
		install -m 755 $(BUILD_DIR)/$(DYNAMIC_LIB_REALNAME) /usr/local/lib/; \
		cd /usr/local/lib/ && $(LN) $(DYNAMIC_LIB_REALNAME) $(DYNAMIC_LIB_SONAME); \
		cd /usr/local/lib/ && $(LN) $(DYNAMIC_LIB_SONAME) $(DYNAMIC_LIB_LINKNAME); \
		ldconfig; \
		echo "动态库安装完成"; \
	else \
		echo "错误: 动态库未构建"; \
	fi

# 安装仅静态库
install-static: static
	@echo "安装静态库到系统目录..."
	@if [ -f "$(BUILD_DIR)/$(STATIC_LIB)" ]; then \
		install -m 644 $(BUILD_DIR)/$(STATIC_LIB) /usr/local/lib/; \
		echo "静态库安装完成"; \
	else \
		echo "错误: 静态库未构建"; \
	fi

# 卸载库文件
uninstall:
	@echo "卸载库文件..."
	$(RM) /usr/local/lib/$(STATIC_LIB)
	$(RM) /usr/local/lib/lib$(TARGET).so*
	$(RM) -r /usr/local/include/$(TARGET)
	ldconfig
	echo "卸载完成"

# 发布版本
release: LOG_LEVEL ?= 2
release: CFLAGS += -DNDEBUG
release: all
	$(STRIP) --strip-debug $(BUILD_DIR)/$(DYNAMIC_LIB_REALNAME)
	$(STRIP) --strip-debug $(BUILD_DIR)/$(STATIC_LIB)
	@echo "=== 发布版本构建完成 ==="

# 调试版本
debug: LOG_LEVEL ?= 5
debug: CFLAGS += -g -DDEBUG
debug: all
	@echo "=== 调试版本构建完成 ==="

# 显示构建信息
info:
	@echo "=== 构建信息 ==="
	@echo "目标: $(TARGET)"
	@echo "版本: $(LIB_VER_MAJOR).$(LIB_VER_MINOR).$(LIB_VER_PATCH)"
	@echo "编译器: $(CC)"
	@echo "归档工具: $(AR)"
	@echo "源文件数量: $(words $(SRCS))"
	@echo "编译模式: $(COMPILE_MODE)"
	@echo "芯片平台: $(SOC_PLATFORM)"
	@echo "交叉编译工具链: $(if $(CROSS_COMPILE),$(CROSS_COMPILE),使用系统gcc)"
	@echo "将生成:"
	@echo "  静态库: $(STATIC_LIB)"
	@echo "  动态库: $(DYNAMIC_LIB_REALNAME)"
	@echo "================="

# 显示文件列表
list:
	@echo "=== 构建目录内容 ==="
	@ls -la $(BUILD_DIR)/
	@echo ""
	@echo "=== 静态库信息 ==="
	@if [ -f "$(BUILD_DIR)/$(STATIC_LIB)" ]; then \
		file $(BUILD_DIR)/$(STATIC_LIB); \
		$(AR) -t $(BUILD_DIR)/$(STATIC_LIB); \
	fi
	@echo ""
	@echo "=== 动态库信息 ==="
	@if [ -f "$(BUILD_DIR)/$(DYNAMIC_LIB_REALNAME)" ]; then \
		file $(BUILD_DIR)/$(DYNAMIC_LIB_REALNAME); \
		$(CROSS_COMPILE)readelf -d $(BUILD_DIR)/$(DYNAMIC_LIB_REALNAME) | grep SONAME; \
	fi

# 显示变量信息
debug-vars:
	@echo "LIB_VER_MAJOR: '$(LIB_VER_MAJOR)'"
	@echo "LIB_VER_MINOR: '$(LIB_VER_MINOR)'" 
	@echo "LIB_VER_PATCH: '$(LIB_VER_PATCH)'"
	@echo "DYNAMIC_LIB_SONAME: '$(DYNAMIC_LIB_SONAME)'"
	@echo "LDFLAGS_DYN: '$(LDFLAGS_DYN)'"

# 测试程序（如果有测试代码）
test: all
	@echo "=== 测试模式 ==="
	@if [ -d "test" ]; then \
		echo "构建测试程序..."; \
		$(CC) -I$(INC_DIRS) -L$(BUILD_DIR) test/test.c -o $(BUILD_DIR)/test_program -l$(TARGET); \
		echo "测试程序: $(BUILD_DIR)/test_program"; \
	else \
		echo "未找到测试目录"; \
	fi

# 显示帮助信息
help:
	@echo "可用目标:"
	@echo "  all             - 同时构建静态库和动态库（默认）"
	@echo "  static          - 仅构建静态库 (.a)"
	@echo "  dynamic         - 仅构建动态库 (.so)"
	@echo "  release         - 构建优化版本"
	@echo "  debug           - 构建调试版本"
	@echo "  clean           - 清理构建文件"
	@echo "  install         - 安装所有库文件"
	@echo "  install-static  - 仅安装静态库"
	@echo "  install-dynamic - 仅安装动态库"
	@echo "  uninstall       - 卸载所有库文件"
	@echo "  info            - 显示构建信息"
	@echo "  list            - 显示构建文件详情"
	@echo "  test            - 构建测试程序（如果有）"
	@echo "  help            - 显示此帮助信息"
	@echo ""
	@echo "编译模式示例:"
	@echo "  make COMPILE_MODE=cross SOC_PLATFORM=rk3568    # 交叉编译 RK3568"
	@echo "  make static                                    # 仅构建静态库"
	@echo "  make dynamic                                   # 仅构建动态库"

.PHONY: all clean info help install uninstall release debug list static dynamic test install-static install-dynamic
